<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>系统管理</title>

    <link rel="stylesheet" href="../../plugins/fontawesome-free/css/all.min.css">
    <link rel="stylesheet" href="../../dist/css/adminlte.min.css">
    <script src="../../dist/js/axios.js"></script>
    <script src="../../dist/js/vue.js"></script>
    <style>
        [v-cloak] {
            display: none;
        }
    </style>
</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper" id="app">
    <!-- Navbar -->
    <!-- Navbar -->
    <nav class="main-header navbar navbar-expand navbar-white navbar-light">
        <!-- Left navbar links -->
        <ul class="navbar-nav">
            <li class="nav-item">
                <a class="nav-link" data-widget="pushmenu" href="#" role="button"><i class="fas fa-bars"></i></a>
            </li>
            <li class="nav-item d-none d-sm-inline-block">
                <a href="#" class="nav-link">首页</a>
            </li>
            <li class="nav-item d-none d-sm-inline-block">
                <a href="#" class="nav-link">联系</a>
            </li>
        </ul>

        <!-- SEARCH FORM -->
        <form class="form-inline ml-3">
            <div class="input-group input-group-sm">
                <input class="form-control form-control-navbar" type="search" placeholder="搜索" aria-label="Search">
                <div class="input-group-append">
                    <button class="btn btn-navbar" type="submit">
                        <i class="fas fa-search"></i>
                    </button>
                </div>
            </div>
        </form>

        <!-- Right navbar links -->
        <ul class="navbar-nav ml-auto">
            <!-- Messages Dropdown Menu -->
            <!-- Notifications Dropdown Menu -->
            <li class="nav-item">
                <a class="nav-link" data-widget="fullscreen" href="#" role="button">
                    <i class="fas fa-expand-arrows-alt"></i>
                </a>
            </li>
        </ul>
    </nav>
    <!-- /.navbar -->
    <!-- Main Sidebar Container -->
    <aside class="main-sidebar sidebar-collapse sidebar-dark-primary elevation-4">
        <!-- Brand Logo -->
        <a href="" class="brand-link">
            <img src="../../dist/img/home.png" alt="AdminLTE Logo" class="brand-image img-circle elevation-3"
                 style="opacity: .8">
            <span class="brand-text font-weight-light">MALL</span>
        </a>
        <!-- Sidebar -->
        <div class="sidebar">
            <!-- Sidebar user panel (optional) -->
            <div class="user-panel mt-3 pb-3 mb-3 d-flex">
                <div class="image">
                    <label for="adminAvatar">
                        <img :src="currentUser.avatar" class="img-circle elevation-2" alt="User Image">
                    </label>
                    <input multiple id="adminAvatar" hidden type="file" name="file" value="上传头像"
                           @change="uploadAdminAvatar"/>
                </div>
                <div class="info">
                    <a href="" class="d-block">{{currentUser.username}}</a>
                </div>
            </div>
            <!-- Sidebar Menu -->
            <nav class="mt-2">
                <ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu"
                    data-accordion="false">
                    <!-- Add icons to the links using the .nav-icon class
                         with font-awesome or any other icon font library -->
                    <li class="nav-item ">
                        <a href="#" class="nav-link ">
                            <i class="nav-icon fas fa-tachometer-alt"></i>
                            <p>
                                用户管理
                                <i class="right fas fa-angle-left"></i>
                            </p>
                        </a>
                        <ul class="nav nav-treeview">
                            <li class="nav-item">
                                <a href="../user/user.html" class="nav-link">
                                    <i class="far fa-user nav-icon"></i>
                                    <p>会员管理</p>
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="../user/address.html" class="nav-link ">
                                    <i class="far fa-address-book nav-icon"></i>
                                    <p>收货地址管理</p>
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="../user/history.html" class="nav-link">
                                    <i class="far fa-history nav-icon"></i>
                                    <p>搜索历史</p>
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="../user/feedback.html" class="nav-link">
                                    <i class="far fa-info  nav-icon"></i>
                                    <p>意见反馈</p>
                                </a>
                            </li>
                        </ul>
                    </li>
                    <li class="nav-item">
                        <a href="#" class="nav-link">
                            <i class="nav-icon fas fa-copy"></i>
                            <p>
                                商城管理
                                <i class="right fas fa-magic"></i>
                                <!--                <i class="fas fa-angle-left right"></i>-->
                                <!--                <span class="badge badge-info right">6</span>-->
                            </p>
                        </a>
                        <ul class="nav nav-treeview">
                            <li class="nav-item">
                                <a href="../mall/order.html" class="nav-link">
                                    <i class="far fa-circle nav-icon"></i>
                                    <p>订单管理</p>
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="../mall/goods.html" class="nav-link">
                                    <i class="far fa-circle nav-icon"></i>
                                    <p>商品管理</p>
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="../mall/common-question.html" class="nav-link">
                                    <i class="far fa-circle nav-icon"></i>
                                    <p>通用问题</p>
                                </a>
                            </li>
                        </ul>
                    </li>
                    <li class="nav-item">
                        <a href="#" class="nav-link">
                            <i class="nav-icon fas fa-chart-pie"></i>
                            <p>
                                推广管理
                                <i class="right fas fa-angle-left"></i>
                            </p>
                        </a>
                        <ul class="nav nav-treeview">
                            <li class="nav-item">
                                <a href="../advertisement/ad.html" class="nav-link">
                                    <i class="far fa-adversal nav-icon"></i>
                                    <p>广告管理</p>
                                </a>
                            </li>
                        </ul>
                    </li>
                    <li class="nav-item menu-open">
                        <a href="#" class="nav-link active">
                            <i class="nav-icon fas fa-receipt"></i>
                            <p>
                                系统管理
                                <i class="fas fa-angle-left right"></i>
                            </p>
                        </a>
                        <ul class="nav nav-treeview">
                            <li class="nav-item">
                                <a href="/views/admin/system/administrator.html" class="nav-link"> <i
                                        class="far fa-user nav-icon"></i>
                                    <p>管理员</p>
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="#" class="nav-link">
                                    <i class="far fa-info-circle nav-icon"></i>
                                    <p>通知管理(未实现)</p>
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="../system/Cloud-Object-Storage.html" class="nav-link ">
                                    <i class="far fa-store-alt nav-icon"></i>
                                    <p>对象存储</p>
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="#" class="nav-link">
                                    <i class="far fa-paper-plane nav-icon"></i>
                                    <p>操作日志(未实现)</p>
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="../system/role.html" class="nav-link active">
                                    <i class="far fa-user nav-icon"></i>
                                    <p>角色管理</p>
                                </a>
                            </li>
                        </ul>
                    </li>
                    <li class="nav-item">
                        <a href="#" class="nav-link">
                            <i class="nav-icon fas fa-edit"></i>
                            <p>
                                配置管理
                                <i class="fas fa-angle-left right"></i>
                            </p>
                        </a>
                        <ul class="nav nav-treeview">
                            <li class="nav-item">
                                <a href="../configuration/mall.html" class="nav-link">
                                    <i class="far fa-circle nav-icon"></i>
                                    <p>商城配置</p>
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="../../pages/forms/general.html" class="nav-link">
                                    <i class="far fa-circle nav-icon"></i>
                                    <p>订单配置(未实现)</p>
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="../../pages/forms/advanced.html" class="nav-link">
                                    <i class="far fa-circle nav-icon"></i>
                                    <p>运费配置(未实现)</p>
                                </a>
                            </li>
                        </ul>
                    </li>
                </ul>
            </nav>
            <!-- /.sidebar-menu -->
        </div>
        <!-- /.sidebar -->
    </aside>


    <!-- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@-->
    <!--  以上均为一致，仅需要微调即可使用-->

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper" v-cloak>
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1>角色管理</h1>
                    </div>
                    <div class="col-sm-6">
                        <ol class="breadcrumb float-sm-right">
                            <li class="breadcrumb-item"><a href="#">首页</a></li>
                            <li class="breadcrumb-item active">系统管理</li>
                        </ol>
                    </div>
                </div>
            </div><!-- /.container-fluid -->
        </section>
        <!-- Main content -->
        <section class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-header">
                                <h3 class="card-title">角色管理</h3>
                                <button type="button" class="btn btn-success  offset-9 col-3" data-toggle="modal"
                                        data-target="#addRoleModal">添加角色
                                </button>
                            </div>
                            <!-- /.card-header -->
                            <div class="card-body">
                                <table id="example1" class="table table-bordered table-striped">
                                    <thead>
                                    <tr>
                                        <th>id</th>
                                        <th>名称</th>
                                        <th>说明</th>
                                        <th>添加时间</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr v-for="item of roles">
                                        <td>{{item.id}}</td>
                                        <td>{{item.name}}</td>
                                        <td>{{item.desc}}</td>
                                        <td>{{item.updateTime}}</td>
                                        <td>
                                            <button type="button" class="col-5 btn btn-primary" data-target="#myModal"
                                                    data-toggle="modal" @click="queryRoleById(item.id)">修改
                                            </button>
                                            <button type="button" class="col-5 btn btn-danger" data-toggle="modal"
                                                    data-target="#deleteModal" @click="setDeletedId(item.id)">删除
                                            </button>
                                        </td>
                                    </tr>
                                    </tbody>
                                </table>
                                <div class="modal fade" id="myModal"
                                     aria-hidden="true">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <button type="button" class="close" data-dismiss="modal"
                                                        aria-label="Close">
                                                    <span aria-hidden="true">&times;</span>
                                                </button>
                                                <h4 class="modal-title"></h4>
                                            </div>
                                            <div class="modal-body">
                                                <div style="">
                                                    <div class="form-group">
                                                        <button type="button" class="form-control btn btn-success"
                                                                data-toggle="modal" data-target="#addPermissionModal">
                                                            添加权限
                                                        </button>
                                                    </div>
                                                    <div class="card-body">
                                                        <div class="form-group">
                                                            <label for="name">名称</label>
                                                            <input type="text" class="form-control"
                                                                   id="name"
                                                                   v-model="theRole.role.name" disabled>
                                                        </div>
                                                        <div class="form-group">
                                                            <label for="desc">说明</label>
                                                            <input type="text" class="form-control"
                                                                   id="desc"
                                                                   v-model="theRole.role.desc">
                                                        </div>

                                                        <div class="form-group">
                                                            <label for="time">加入时间</label>
                                                            <input type="datetime-local" class="form-control"
                                                                   id="time"
                                                                   v-model="theRole.role.updateTime" disabled>
                                                        </div>
                                                        <div>
                                                            <table id="permissions"
                                                                   class="table table-bordered table-striped">
                                                                <thead>
                                                                <tr>
                                                                    <th>id</th>
                                                                    <th>权限</th>
                                                                    <th>添加时间</th>
                                                                    <th>操作</th>
                                                                </tr>
                                                                </thead>
                                                                <tbody>
                                                                <tr v-for="item of theRole.permissions">
                                                                    <td>{{item.id}}</td>
                                                                    <td>{{item.permission}}</td>
                                                                    <td>{{item.updateTime}}</td>
                                                                    <td>
                                                                        <button type="button"
                                                                                class="col-12 btn btn-danger"
                                                                                data-toggle="modal"
                                                                                data-target="#deletePermissionModal"
                                                                                @click="deletedPermissionId=item.id">
                                                                            删除
                                                                        </button>
                                                                    </td>
                                                                </tr>
                                                                </tbody>
                                                            </table>
                                                            <div class="modal fade" id="deletePermissionModal"
                                                                 aria-hidden="true">
                                                                <div class="modal-dialog">
                                                                    <div class="modal-content">
                                                                        <div class="modal-body">
                                                                            <div style="">
                                                                                <!-- /.card-body -->
                                                                                <div class="card-footer">
                                                                                    <button type="button"
                                                                                            class="btn btn-default"
                                                                                            data-dismiss="modal">取消
                                                                                    </button>
                                                                                    <button type="button"
                                                                                            class="btn btn-danger"
                                                                                            @click="deletePermissionById()">
                                                                                        删除
                                                                                    </button>
                                                                                </div>
                                                                            </div>
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="modal fade" id="addPermissionModal"
                                     aria-hidden="true">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <div class="modal-body">
                                                <div style="">
                                                    <!-- /.card-body -->
                                                    <div class="form-group">
                                                        <label for="aNewPermission">新权限</label>
                                                        <input id="aNewPermission" type="text" class="form-control"
                                                               v-model="aNewPermission" placeholder="请输入权限名称"/>
                                                    </div>

                                                    <div class="card-footer">
                                                        <button type="button" class="btn btn-success col-12"
                                                                data-dismiss="modal" @click="addNewPermission()">确认
                                                        </button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="modal fade" id="deleteModal"
                                     aria-hidden="true">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <div class="modal-body">
                                                <div style="">
                                                    <!-- /.card-body -->
                                                    <div class="card-footer">
                                                        <button type="button" class="btn btn-default"
                                                                data-dismiss="modal">取消
                                                        </button>
                                                        <button type="button" class="btn btn-danger"
                                                                @click="deleteRoleById()">删除
                                                        </button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div class="modal fade" id="addRoleModal"
                                     aria-hidden="true">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <button type="button" class="close" data-dismiss="modal"
                                                        aria-label="Close">
                                                    <span aria-hidden="true">&times;</span>
                                                </button>
                                                <h4 class="modal-title"></h4>
                                            </div>
                                            <div class="modal-body">
                                                <div style="">
                                                    <div class="card-body">
                                                        <div class="form-group">
                                                            <label for="roleName">名称</label>
                                                            <input type="text" class="form-control"
                                                                   id="roleName"
                                                                   v-model="aNewRole.name">
                                                        </div>
                                                        <div class="form-group">
                                                            <label for="aNewRoleDesc">说明</label>
                                                            <input type="text" class="form-control"
                                                                   id="aNewRoleDesc"
                                                                   v-model="aNewRole.desc">
                                                        </div>
                                                    </div>
                                                    <div class="card-footer">
                                                        <button type="button" class="btn btn-default col-5"
                                                                data-dismiss="modal">取消
                                                        </button>
                                                        <button type="button" class="btn btn-success col-5"
                                                                @click="addNewRole()">添加
                                                        </button>
                                                    </div>
                                                </div>
                                            </div>

                                        </div>
                                    </div>
                                </div>

                            </div>
                            <!-- /.card-body -->
                        </div>
                        <!-- /.card -->
                    </div>
                    <!-- /.col -->
                </div>
                <!-- /.row -->
            </div>
            <!-- /.container-fluid -->
        </section>
        <!-- /.content -->
    </div>
    <!--  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@-->
    <!--  以下均为一致，仅需微调即可使用-->
    <!-- /.content-wrapper -->
    <footer class="main-footer">
        <strong>Copyright &copy; 2020 <a href="">MALL</a></strong>
        All rights reserved.
        <div class="float-right d-none d-sm-inline-block">
            <b>Version</b> v1.0.0
        </div>
    </footer>


    <!-- Control Sidebar -->
    <aside class="control-sidebar control-sidebar-dark">
        <!-- Control sidebar content goes here -->
    </aside>
    <!-- /.control-sidebar -->
</div>
<!-- ./wrapper -->

<!-- jQuery -->
<script src="../../plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="../../plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="../../dist/js/adminlte.min.js"></script>
<script src="/views/admin/js/common.js"></script>

<script>
    var data = {
        roles: [],
        deletedId: -1,
        deletedPermissionId: -1,
        theRole: {
            role: {},
            permissions: {}
        },
        aNewPermission: "",
        aNewRole: {},
        currentUser: {}
    };
    var methods = {
        uploadAdminAvatar(event) {
            updateAdminAvatar(event);
        },
        setDeletedId: function (id) {
            this.deletedId = id;
        },
        deleteRoleById: function () {
            axios.delete("/role/deleteById/" + this.deletedId, {
                headers: {
                    Authorization: adminAccessToken
                }
            })
                .then(function (response) {
                    if (response.data.code === 200) {
                        alert("删除成功！");
                        $("#deleteModal").modal("hide");
                        mounted();
                    }
                }).catch(function (err) {
                console.log(err);
            })
        },
        deletePermissionById: function () {
            axios.delete("/permission/deleteById/" + this.deletedPermissionId, {
                headers: {
                    Authorization: adminAccessToken
                }
            })
                .then(function (response) {
                    if (response.data.code === 200) {
                        alert("删除成功！");
                        $("#deletePermissionModal").modal("hide");
                        this.app.queryRoleById(this.app.theRole.role.id);
                    }
                }).catch(function (err) {
                console.log(err);
            })
        },
        queryRoleById: function (id) {
            axios.get("/role/queryById/" + id, {
                headers: {
                    Authorization: adminAccessToken
                }
            }).then(function (response) {
                if (response.data.code === 200) {
                    this.app.theRole = response.data.data;
                }
            }).catch(function (err) {
                console.log(err);
            })
        },
        addNewPermission: function () {
            axios.post("/permission/add", {
                roleId: this.theRole.role.id,
                permission: this.aNewPermission
            }, {
                headers: {
                    Authorization: adminAccessToken
                }
            }).then(function (response) {
                alert("添加成功！");
                this.app.queryRoleById(this.app.theRole.role.id);
            })
        },
        addNewRole: function () {
            axios.post("/role/add", {
                name: this.aNewRole.name,
                desc: this.aNewRole.desc
            }, {
                headers: {
                    Authorization: adminAccessToken
                }
            }).then(function (response) {
                if (response.data.code === 200) {
                    alert("添加成功！");
                    $("#addRoleModal").modal("hide");
                    mounted();
                }
            }).catch(function (err) {
                console.log(err);
            })
        }
    };
    var mounted = function () {
        retrieveAdminByToken();
        axios.get("/role/listSearchAll", {
            headers: {
                Authorization: adminAccessToken
            }
        })
            .then(function (response) {
                if (response.data.code === 200) {
                    this.app.roles = response.data.data;
                }
            }).catch(function (err) {
            console.log(err);
        })
    }
    var app = new Vue({
        el: "#app",
        data: data,
        methods: methods,
        mounted: mounted
    });
</script>
</body>
</html>
